package com.anke.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.anke.constant.MessageConstant;
import com.anke.entity.Company;
import com.anke.entity.Manager;
import com.anke.entity.Result;
import com.anke.entity.User;
import com.anke.service.ManagerService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manager")
@CrossOrigin
public class ManagerController {
    @Reference
    private ManagerService managerService;

    @PostMapping("/login")
    public Result login(@RequestBody Manager manager) {
        System.out.println("请求登录");
        if (managerService.login(manager)) {
            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        } else {
            return new Result(false, MessageConstant.LOGIN_FAIL);
        }
    }

    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        try {
            managerService.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SUCCESS);
    }

    @RequestMapping("/deleteUser")
    public Result deleteUser(@RequestBody User user) {
        try {
            managerService.deleteUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SUCCESS);
    }

    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        try {
            if (managerService.updateUser(user)){
                return new Result(true,MessageConstant.UPDATE_SUCCESS);
            }
            return new Result(false, MessageConstant.UPDATE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SYSTEM_EXCEPTION);
        }
    }
    @RequestMapping("/selectUser")
    public Result selectUser() {
        List<User> u = managerService.selectUser();
        if (u.isEmpty()) {
            return new Result(false, MessageConstant.SELECT_FAIL);

        } else {
            return new Result(true, MessageConstant.SELECT_SUCCESS,u);
        }
    }

    @RequestMapping("/addCompany")
    public Result addCompany(@RequestBody Company company) {
        try {
            managerService.addCompany(company);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SUCCESS);
    }

    @RequestMapping("/deleteCompany")
    public Result deleteCompany(@RequestBody Company company) {
        try {
            managerService.deleteCompany(company);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SUCCESS);
    }

    @RequestMapping("/updateCompany")
    public Result updateCompany(@RequestBody Company company) {
        try {
            if (managerService.updateCompany(company)){
                return new Result(true,MessageConstant.UPDATE_SUCCESS);
            }
            return new Result(false, MessageConstant.UPDATE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SYSTEM_EXCEPTION);
        }
    }

    @RequestMapping("/selectCompany")
    public Result selectCompany() {
        List<Company> u = managerService.selectCompany();
        if (u.isEmpty()) {
            return new Result(false, MessageConstant.SELECT_FAIL);
        } else {
            return new Result(true, MessageConstant.SELECT_SUCCESS,u);
        }
    }
}
